package com.hbpu.encap.list;

import com.hbpu.encap.arrays.ArrayExercise;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ListExercise02 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        Book book = new Book();

        arrayList.add(new Book("红楼梦", "曹雪芹", 100));
        arrayList.add(new Book("西游记", "吴承恩", 10));
        arrayList.add(new Book("水浒传", "施耐庵", 19));
        arrayList.add(new Book("三国演义", "罗贯中", 80));

        for (int i = 0; i < arrayList.size() - 1; i++) {
            for (int j = 0; j < arrayList.size() - 1 - i; j++) {
                if (((Book)arrayList.get(j)).getPrice() > ((Book)arrayList.get(j + 1)).getPrice()) {
                    Book temp = (Book) arrayList.get(j);
                    arrayList.set(j, arrayList.get(j + 1));
                    arrayList.set(j + 1, temp);
                }
            }
        }

        Iterator iterator = arrayList.iterator();

        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println(next);
        }
    }
}

class Book {
    private String bookName;
    private String author;
    private int price;

    public Book(String bookName, String author, int price) {
        this.bookName = bookName;
        this.author = author;
        this.price = price;
    }

    public Book() {
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "名称：" + bookName + "\t\t" +
                "价格：" + price + "\t\t" +
                "作者：" + author;
    }
}